/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SquareFighters;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 *
 * @author Someperson1
 */
public class Enemy {

    public int h;
    public static int mh;
    int[] pos = new int[2];
    Field field;
    Rectangle bounds;

    public Enemy(Field field) {
        mh = field.level * 2;
        h = mh;
        this.field = field;
        bounds = new Rectangle(0, 0, 10, 10);
        Rectangle limit = new Rectangle(field.pos[0] - 100, field.pos[1] - 100, 200, 200);
        do {
            pos[0] = (int) (Math.random() * (field.getWidth() - 1));
            pos[1] = (int) (Math.random() * (field.getHeight() - 1));
            checkPos();
            bounds.setLocation(pos[0] - 5, pos[1] - 5);
        } while (bounds.intersects(limit));
    }

    public void detectCollision() {
        if (bounds.intersects(field.actor.bounds)) {
            modh(-1);
            field.score -= 10;
            field.actor.modHealth(((-1 * field.level) / 10) - 1);
            field.hframe = -10;
        }
        if (field.actor.magic != null) {
            if (bounds.intersects(field.actor.magic.bounds)) {
                if (field.actor.magic.uber) {
                    modh(-20);
                    field.score += 200;
                } else {
                    modh(-10);
                    field.score += 100;
                    field.actor.magic = null;
                    field.actor.usem = false;
                }
            }
        }
        if (field.actor.sword != null) {
            if (bounds.intersects(field.actor.sword.bounds)) {
                modh(-20);
                field.score += 200;
                field.actor.sword.uses++;
                if (field.actor.sword.uses >= 5) {
                    field.actor.sword = null;
                    field.actor.uses = false;
                }
            }
        }
    }

    public void modh(int h) {
        this.h += h;
        if (this.h <= 0) {
            this.h = 0;
        }
    }

    private void checkPos() {
        if (pos[0] < 0) {
            pos[0] = field.getWidth() - 1;
        }
        if (pos[0] > field.getWidth() - 1) {
            pos[0] = 0;
        }
        if (pos[1] < 15) {
            pos[1] = field.getHeight() - 1;
        }
        if (pos[1] > field.getHeight() - 1) {
            pos[1] = 15;
        }
    }

    public void draw(Graphics g) {
        for (int i = 0; i < pos.length; i++) {
            if (pos[i] > field.pos[i]) {
                pos[i] -= 2;
            } else if (pos[i] < field.pos[i]) {
                pos[i] += 2;
            }
        }
        checkPos();
        bounds.setLocation(pos[0] - 5, pos[1] - 5);
        detectCollision();
        g.setColor(Color.ORANGE);
        g.fillRect(pos[0] - 5, pos[1] - 5, 10, 10);
        g.setColor(Color.RED);
        g.fillRect(pos[0] - 10, pos[1] - 10, 20 * h / mh, 2);
    }
}
